<?php
apf_require_controller("QaAbstract");
apf_require_class("Bll_Qa_Question");
class Qa_Finance_QaAddController extends QaAbstractController{

    public function handle_request(){
        $params = $this->request->get_parameters();
        $qid = empty($params['qid']) ? 0 : intval($params['qid']);
        $params['type'] = 1;
        $cate = empty($params['cate']) ? 0 : intval($params['cate']);

        //获取二级标题的名称
        $cate_name = Const_Qa::get_left_name_by_id($cate);
        if(!$cate_name){
            $this->show_tips('ERROR_SYSTEM');
        }

        //权限判断
        $tag = Const_Qa::get_menu_list_en(Const_Qa::TAG_FINANCE);
        if(!$_SESSION[$tag]){
            $this->show_tips("ERROR_PERMISSION", $_SERVER['HTTP_REFERER']);
        }

        $qa_bll  = new Bll_Qa_Question();
        if($qid){
            $data = $qa_bll->get_question_one($qid);
            if(empty($data)){
                $this->show_tips('ERROR_PARAM');
            }
        }
        if($this->request->is_post_method()){
            $this->_check_params($params);
            if($qid){
                $updata = array(
                        'file'=>'',
                        'type'=>intval($params['type']),
                        'cate'=>intval($params['cate']),
                        'title'=>trim(htmlspecialchars($params['title'])),
                        'updater'=>$this->get_user_id(),
                        );
                $qa_bll->update_question($qid,$updata['title'],$updata['file'],$data['hits'],$this->get_user_id());
                $qa_bll->update_answer($qid,$params['content']);
            }else{
                $qid = $qa_bll->add_question($params['type'],$params['cate'],$params['title'],'',$this->get_user_id());
                $qa_bll->insert_answer($qid,$params['content']);
            }
            $this->response->redirect(Util_BaseUrls::finance_list_url($params['cate']));
        }
        $this->request->set_attribute('params',$params);
        $this->request->set_attribute('data',$data);
        $this->request->set_attribute('cate_name',$cate_name);
        return "Qa_Finance_QaAdd";

    }

    private function _check_params($params){
        $params['title'] = trim($params['title']);
        if(empty($params['content']) || empty($params['type'])  || empty($params['cate']) || empty($params['title'])){
            $this->show_tips('ERROR_PARAM');
        }
    }
}